package com.cskaoyan.controller.system;

import com.cskaoyan.bean.MallStorage;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.fileupload.FileUploadData;
import com.cskaoyan.model.vo.PageListVo;
import com.cskaoyan.service.system.StorageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @program: project-market
 * @description: 对象存储管理
 * @author: Weiwei Liu
 * @create: 2021-11-30 11:45
 **/
@RestController
@RequestMapping("admin/storage")
public class StorageController {
    @Value("${upload.path}")
    String path;


    @Autowired
    StorageService storageService;

    /**
     * 显示所有对象列表
     *
     * @return
     */

    @RequiresPermissions("admin:storage:list")
    @RequestMapping("list")
    public BaseRespVo list(PageBo pageBo, String key, String name) {

        PageListVo vo = storageService.list(pageBo, key, name);

        return BaseRespVo.ok(vo);

    }

    /**
     * 创建对象信息
     *
     * @return
     */
//    @RequestMapping("create")
//    public BaseRespVo create(MallStorage mallStorage) {
//
//        MallStorage newStorage = storageService.create(mallStorage);
//
//        return BaseRespVo.ok(newStorage);
//    }
    //商品图片文件上传  by GJC   url: admin/storage/create
    //tips: 形参不同可以重载
    @RequiresPermissions("admin:storage:create")
    @PostMapping("create")
    public BaseRespVo create(HttpServletRequest httpServletRequest, MultipartFile file) throws IOException {

//           原始上传文件名
        String originalFileName = file.getOriginalFilename();//name
//            获取文件格式
        String contentType = file.getContentType();//type
//           获取文件大小
        int fileSize = ((int) file.getSize());//size
        //文件创建时间
        Date now = new Date();
        //        获取url
        String storageFileName = UUID.randomUUID().toString().replace("-", "");
        System.out.println(httpServletRequest.getRequestURL());//http://localhost:8083/admin/storage/create  还需加上文件名+.jpg
        if (new File(path).mkdirs()) {
            new File(path).mkdirs();
        }
        //生成文件地址
        File storageFile = new File(path, storageFileName + ".jpg");

        //注入
        file.transferTo(storageFile);

        FileUploadData fileUploadData = new FileUploadData(null, storageFileName, originalFileName, contentType, fileSize,
                //httpServletRequest.getRequestURL().toString()+"/"+storageFileName + ".jpg"
                //注意这里是localAddr()本地IP地址,而非是获取客户端地址RemoteAddr
                        httpServletRequest.getRequestURL().toString().replace(httpServletRequest.getRequestURI(),"")
//                    "http://"+httpServletRequest.getRemoteHost()+":8083"
                + "/wxhomeindex/storage/fetch/"+storageFileName+".jpg"
                , now, now);
        MallStorage storage = storageService.uploadPic(fileUploadData);
        return BaseRespVo.ok(storage);
    }

    /**
     * 更新对象信息
     *
     * @return
     */
    @RequiresPermissions("admin:storage:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MallStorage mallStorage) {

        Boolean status = storageService.update(mallStorage);

        if (status) return BaseRespVo.ok();

        return BaseRespVo.fail("更新失败");
    }

    /**
     * 删除对象信息
     *
     * @return
     */
    @RequiresPermissions("admin:storage:delete")

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MallStorage mallStorage) {

        Boolean status = storageService.delete(mallStorage);

        if (status) return BaseRespVo.ok();

        return BaseRespVo.fail("删除失败");

    }

}
